package com.dm.gateway.config;

import com.dm.commons.util.ResourceIdUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;

import javax.annotation.Resource;

/**
 *
 * 资源服务器配置
 * **/
@Configuration
public class ResourceServerConfig {

    /**
     * uaa资源服务配置
     * */
    @Configuration
    @EnableResourceServer
    public static class UAAServerConfig extends ResourceServerConfigurerAdapter {
        @Resource
        private TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources){
            resources.tokenStore(tokenStore).resourceId(ResourceIdUtil.GATEWAY_RESOURCE_ID)
                    .stateless(true);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                 .antMatchers("/uaa/**").permitAll();

        }
    }

    /**
     * user资源服务配置
     * */
    @Configuration
    @EnableResourceServer
    public static class OrderServerConfig extends ResourceServerConfigurerAdapter {
        @Resource
        private TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources){
            resources.tokenStore(tokenStore).resourceId(ResourceIdUtil.USER_RESOURCE_ID)
                    .stateless(true);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/user/**").access("#oauth2.hasScope('SERVICE_USER')");
        }
    }


    //配置其它的资源服务..


}
